package com.dotstone.chipism.bean;

import java.util.ArrayList;
import java.util.List;

import com.dotstone.chipism.util.ConvertUtil;
import com.dotstone.chipism.util.SocketManager;
import com.dotstone.chipism.util.Util;

import android.util.Log;

public class Cmd {
	private String mainDeviceId;
	private String openCmdss;
	private String closeCmdss;
	private String openCmdl;
	private String closeCmdl;
	private List<Device> mSSDevices;
	private List<Device> mLightDevices;
	private List<String> mLightAddress = new ArrayList<String>();
	private List<String> mSwitchAddress = new ArrayList<String>();
	private int[] intOpenSwitchAdds;
	private int[] intCloseSwitchAdds;
	private int[] intOpenLightAdds;
	private int[] intCloseLightAdds;
	private byte[] byteSocketAdds;
	private byte[] byteLightAdds;
	private int[] intLS = new int[5];
	private int[] intLA = new int[5];

	public String getMainDeviceId() {
		return mainDeviceId;
	}

	public void setMainDeviceId(String mainDeviceId) {
		this.mainDeviceId = mainDeviceId;
	}
	

	public Cmd() {

	}

	public String getOpenCmdss() {
		return openCmdss;
	}

	public String getCloseCmdss() {
		return closeCmdss;
	}

	public String getOpenCmdl() {
		return openCmdl;
	}

	public String getCloseCmdl() {
		return closeCmdl;
	}

	public List<Device> getmSSDevices() {
		return mSSDevices;
	}

	public void setmSSDevices(List<Device> mSSDevices) {
		this.mSSDevices = mSSDevices;
		if (mSSDevices.size() > 0) {
			for (int i = 0; i < mSSDevices.size(); i++) {
				mSwitchAddress.add(mSSDevices.get(i).getDeviceAddress());
			}
			intOpenSwitchAdds = new int[mSwitchAddress.size() * 5 + 5];
			intCloseSwitchAdds = new int[mSwitchAddress.size() * 5 + 5];
			intOpenSwitchAdds[0] = mSwitchAddress.size();
			intCloseSwitchAdds[0] = mSwitchAddress.size();
			intOpenSwitchAdds[mSwitchAddress.size() * 5 + 5 - 4] = 16;
			intOpenSwitchAdds[mSwitchAddress.size() * 5 + 5 - 3] = 0;
			intOpenSwitchAdds[mSwitchAddress.size() * 5 + 5 - 2] = 0;
			intOpenSwitchAdds[mSwitchAddress.size() * 5 + 5 - 1] = 0;
			intCloseSwitchAdds[mSwitchAddress.size() * 5 + 5 - 4] = 16;
			intCloseSwitchAdds[mSwitchAddress.size() * 5 + 5 - 3] = 255;
			intCloseSwitchAdds[mSwitchAddress.size() * 5 + 5 - 2] = 0;
			intCloseSwitchAdds[mSwitchAddress.size() * 5 + 5 - 1] = 0;
			for (int i = 0; i < mSwitchAddress.size(); i++) {
				byteSocketAdds = Util.hexStringToBytes(mSwitchAddress.get(i));
				intLS = Util.byte2HexStr1(byteSocketAdds);
				for (int j = 0; j < intLS.length; j++) {
					intOpenSwitchAdds[i * 5 + 1 + j] = intLS[j];
					intCloseSwitchAdds[i * 5 + 1 + j] = intLS[j];
				}
			}
			closeCmdss = ConvertUtil.getInstance().transGroundCmd(intCloseSwitchAdds,mainDeviceId);
			openCmdss =  ConvertUtil.getInstance().transGroundCmd(intOpenSwitchAdds,mainDeviceId);
		} else {
			closeCmdss = "";
			openCmdss = "";
		}
	}

	public void setOpenCmdss(String openCmdss) {
		this.openCmdss = openCmdss;
	}

	public void setCloseCmdss(String closeCmdss) {
		this.closeCmdss = closeCmdss;
	}

	public List<Device> getmLightDevices() {
		return mLightDevices;
	}

	public void setmLightDevices(List<Device> mLightDevices) {
		this.mLightDevices = mLightDevices;
		Log.i("wmy", "mLightDevices = "+mLightDevices.size());
		if (mLightDevices.size() > 0) {
			for (int i = 0; i < mLightDevices.size(); i++) {
				mLightAddress.add(mLightDevices.get(i).getDeviceAddress());
			}
			intOpenLightAdds = new int[mLightAddress.size() * 5 + 5];
			intCloseLightAdds = new int[mLightAddress.size() * 5 + 5];
			intOpenLightAdds[0] = mLightAddress.size();
			intCloseLightAdds[0] = mLightAddress.size();
			intOpenLightAdds[mLightAddress.size() * 5 + 5 - 4] = 121;
			intOpenLightAdds[mLightAddress.size() * 5 + 5 - 3] = 255;
			intOpenLightAdds[mLightAddress.size() * 5 + 5 - 2] = 0;
			intOpenLightAdds[mLightAddress.size() * 5 + 5 - 1] = 0;
			intCloseLightAdds[mLightAddress.size() * 5 + 5 - 4] = 120;
			intCloseLightAdds[mLightAddress.size() * 5 + 5 - 3] = 0;
			intCloseLightAdds[mLightAddress.size() * 5 + 5 - 2] = 0;
			intCloseLightAdds[mLightAddress.size() * 5 + 5 - 1] = 0;
			for (int i = 0; i < mLightAddress.size(); i++) {
				byteLightAdds = Util.hexStringToBytes(mLightAddress.get(i));
				intLA = Util.byte2HexStr1(byteLightAdds);
				for (int j = 0; j < intLA.length; j++) {
					intOpenLightAdds[i * 5 + 1 + j] = intLA[j];
					intCloseLightAdds[i * 5 + 1 + j] = intLA[j];
				}
			}
			closeCmdl = ConvertUtil.getInstance().transGroundCmd(intCloseLightAdds,mainDeviceId);
			openCmdl = ConvertUtil.getInstance().transGroundCmd(intOpenLightAdds, mainDeviceId);
		} else {
			closeCmdl = "";
			openCmdl = "";
		}
	}

}
